package struts.action;

import java.io.IOException;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import struts.form.ActionForm;
import struts.form.FullForm;
import struts.form.XmlBean;

public class ActionServlet extends HttpServlet {

	private static final long serialVersionUID = -7127150859111716940L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		String name = request.getParameter("name");
//		request.setAttribute("name", name);
//		//获得转发器对象
//		RequestDispatcher rd = request.getRequestDispatcher("/view/a.jsp");
//		rd.forward(request, response);
		
		String path = request.getServletPath().split(".")[0];
		//struts是在xml里面配的监听器
		Map<String, XmlBean> map = (Map<String, XmlBean>) this.getServletContext().getAttribute("struts");
		XmlBean xml = map.get(path);
		String formclass = xml.getBeanName();
		ActionForm form = FullForm.full(formclass, request);
		String  actiontype= xml.getActionType();
		Action action = null;
		String url = "";
		try{
			Class clazz = Class.forName(actiontype);
			action = (Action) clazz.newInstance();
			url = action.execute(request, form, xml.getActionForward());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		RequestDispatcher rd = request.getRequestDispatcher(url);
		rd.forward(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

}
